package someTestExcemple.huaweiOd.logicAnalysis;
//华为OD机试 - 流浪地球 - 感谢@on your left反馈（Java 2025 A卷 100分）
//https://blog.csdn.net/guorui_java/article/details/141440085
import java.util.*;
public class WanderingEarth {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt(); //发动机总数
        int E = sc.nextInt(); //手工启动发动机数
        int[] arr = new int[N];
        for(int i=0;i<N;i++) {
            arr[i]=Integer.MAX_VALUE; //默认都没启动
        }
        boolean[] visited = new boolean[N];
        int currentTime = Integer.MAX_VALUE;
        for(int i=0;i<E;i++) {
            int time = sc.nextInt();
            int index = sc.nextInt();
            arr[index] = time; //启动时间
            currentTime = Math.min(currentTime,time);
        }
        int startNum = 0;
        Map<Integer,List<Integer>> startMap = new HashMap<>();
        List<Integer> result;
        while(true) {
            if(startNum == N) {
                result = startMap.get(currentTime);
                break;
            }
            for(int i=0;i<N;i++) {
                if(arr[i] == currentTime && !visited[i]) {
                    startNum++;
                    visited[i] = true;
                    //如果键currentTime不存在，插入一条数据
                    //<currentTime,new ArrayList<>()> ，如果存在不做任何操作
                    startMap.putIfAbsent(currentTime,new ArrayList<>());
                    //现在键已经存在了
                    startMap.get(currentTime).add(i);


                }
            }
            //启动后，判断是否全启动
            if(startNum == N) {
                result = startMap.get(currentTime);
                break;
            }
            List<Integer> initStart = startMap.get(currentTime);
            //下一时刻-联动启动
            currentTime++;
            List<Integer> next = new ArrayList<>();
            for(Integer i : initStart) {
                int index1 =(i-1+N)%N;
                int index2 =(i+1+N)%N;
                if(!visited[index1]) {
                    arr[index1] = currentTime;
                }
                if(!visited[index2]) {
                    arr[index2] = currentTime;
                }
            }

        }
        System.out.println(result.size());
        StringJoiner joiner = new StringJoiner(" ");
        for(Integer i : result) {
            joiner.add(String.valueOf(i));
        }
        System.out.println(joiner);


    }
}
